package com.huawei.zyb.taskManager.domain.task.valueobject;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * 预估工时值对象
 * 范围：≥0，精度：保留两位小数，单位：小时
 */
public final class EstimatedHours {
    private static final BigDecimal MIN_HOURS = BigDecimal.ZERO;
    private static final int SCALE = 2;
    
    private final BigDecimal value;

    private EstimatedHours(BigDecimal value) {
        this.value = Objects.requireNonNull(value, "预估工时不能为空")
                .setScale(SCALE, RoundingMode.HALF_UP);
        
        if (this.value.compareTo(MIN_HOURS) < 0) {
            throw new IllegalArgumentException("预估工时必须为非负数");
        }
    }

    public static EstimatedHours of(BigDecimal value) {
        return new EstimatedHours(value);
    }

    public static EstimatedHours of(double value) {
        return new EstimatedHours(BigDecimal.valueOf(value));
    }

    public static EstimatedHours zero() {
        return new EstimatedHours(MIN_HOURS);
    }

    public BigDecimal getValue() {
        return value;
    }

    public boolean isZero() {
        return value.compareTo(MIN_HOURS) == 0;
    }

    /**
     * 增加工时
     */
    public EstimatedHours add(EstimatedHours other) {
        return new EstimatedHours(this.value.add(other.value));
    }

    /**
     * 比较工时大小
     */
    public boolean isGreaterThan(EstimatedHours other) {
        return this.value.compareTo(other.value) > 0;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        EstimatedHours that = (EstimatedHours) obj;
        return Objects.equals(value, that.value);
    }

    @Override
    public int hashCode() {
        return Objects.hash(value);
    }

    @Override
    public String toString() {
        return "EstimatedHours{" + value + "h}";
    }
}